package eoeProject.edenOfEast.page.analyses.analysesRes.component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import eoeProject.edenOfEast.bean.EOEAnalysesGJBean;
import eoeProject.edenOfEast.core.DBConnector;
import eoeProject.edenOfEast.core.SQLMaker;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES_GJ;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES_RES;
import eoeProject.edenOfEast.utils.EOEUtils;

//@AuthorizeInstantiation("ADMIN")
public class AnalysesGJUserListDialogPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private Integer resId;

	private AnalysesResListPanel parent;

	public AnalysesGJUserListDialogPanel(String id, Integer resId,
			AnalysesResListPanel parent) {
		super(id);
		this.resId = resId;
		this.parent = parent;

		add(closeButton.setOutputMarkupId(true));

		List<EOEAnalysesGJBean> gjList = new ArrayList<EOEAnalysesGJBean>();

		SQLMaker sql = new SQLMaker(DB_EOE_ANALYSES_GJ.TABLE_NAME,
				SQLMaker.SQL_TYPE_SELECT);
		sql.addExpEquals(DB_EOE_ANALYSES_GJ.RES_ID, resId);
		sql.addOrderBy(DB_EOE_ANALYSES_GJ.UPDATE_DATE, "desc");
		ResultSet rs = DBConnector.getInstance().get(sql.getSQL());
		try {
			while (rs.next()) {
				EOEAnalysesGJBean bean = new EOEAnalysesGJBean();
				bean.setResId(rs.getInt(DB_EOE_ANALYSES_RES.RES_ID));
				bean.setUserId(rs.getInt(DB_EOE_ANALYSES_GJ.USER_ID));
				bean.setUpdateDate(rs
						.getTimestamp(DB_EOE_ANALYSES_GJ.UPDATE_DATE));

				gjList.add(bean);
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println("AnalysesGJUserListDialogPanelException: "
					+ e.getMessage());
		}

		ListView<Object> gjUserList = new ListView<Object>("gjUserList", gjList) {
			private static final long serialVersionUID = -6962499153966966392L;

			@SuppressWarnings("rawtypes")
			@Override
			protected void populateItem(ListItem item) {
				final EOEAnalysesGJBean bean = (EOEAnalysesGJBean) item
						.getModelObject();
				PageParameters parameters = new PageParameters();
				parameters.add("userId", bean.getUserId());
				BookmarkablePageLink<WebPage> accountLink = new BookmarkablePageLink<WebPage>(
						"accountLink",
						eoeProject.edenOfEast.page.account.accountDetailPage.Index.class,
						parameters);
				accountLink.add(new Label("createUserName", EOEUtils
						.getUserName(bean.getUserId())));
				item.add(accountLink);

				SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日 H時m分s秒");
				item.add(new Label("updateDate", sdf.format(bean
						.getUpdateDate())));

			}

		};
		add(gjUserList);
	}

	private AjaxLink<Void> closeButton = new AjaxLink<Void>("closeButton") {
		/**
		 *
		 */
		private static final long serialVersionUID = -2942520388218435030L;

		@Override
		public void onClick(AjaxRequestTarget target) {
			setResponsePage(target.getPage());
			parent.closeDialog();
			target.add();
		}

	};

}
